'use strict';

const BaseController = require('./base.js');

class GoodsTypeController extends BaseController {
  
    async index() {
 
        let result = await this.ctx.model.GoodsType.find({});

        await this.ctx.render('admin/goodsType/index', {
            list: result
        });
    }

    // 添加商品页面
    async add() {
        await this.ctx.render('admin/goodsType/add')
    }

    // 添加商品
    async doAdd() {

        let goods = new this.ctx.model.GoodsType(this.ctx.request.body);

        await goods.save();
        await this.success('/admin/goodsType', '增加商品成功');
        
    }

    // 编辑商品页面
    async edit() {
        let id = this.ctx.query.id;
        let result = await this.ctx.model.GoodsType.find({"_id":id})
        await this.ctx.render('admin/goodsType/edit', {
            list: result[0]
        })
    }

    // 编辑商品提交
    async doEdit() {
        var _id = this.ctx.request.body._id;
        var title = this.ctx.request.body.title;
        var description = this.ctx.request.body.description;
        
        await this.ctx.model.GoodsType.updateOne({"_id":_id}, {title, description})
        await this.success('/admin/goodsType', '编辑商品成功');
    }
}

module.exports = GoodsTypeController;
